package com.nsqt.biz.controller;

import com.nsqt.biz.domain.InfocollDO;
import com.nsqt.biz.domain.SoftwareDO;
import com.nsqt.biz.service.AgentService;
import com.nsqt.biz.service.InfocollService;
import com.nsqt.biz.service.SoftwareService;
import com.nsqt.common.utils.PageUtils;
import com.nsqt.common.utils.Query;
import com.nsqt.common.utils.R;
import com.nsqt.common.utils.ShiroUtils;
import com.nsqt.system.domain.UserDO;
import com.nsqt.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 软著信息采集
 */
@Controller
@RequestMapping("/biz/infocomplete")
public class InfocompleteController {
    @Autowired
    private InfocollService infocollService;
    @Autowired
    private UserService userService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private SoftwareService softwareService;

    @GetMapping()
    String Infocoll() {
        return "biz/infocomplete/infocomplete";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据

        Query query = new Query(params);
        Long currUserId = ShiroUtils.getUserId();
        UserDO currUser = userService.get(currUserId);
        List<Long> roles = currUser.getRoleIds();

        if ((roles.contains(new Long(6)))) {
            query.put("material", currUserId);
        }

        List<InfocollDO> infocollList = infocollService.listComp(query);
        int total = infocollService.countComp(query);
        PageUtils pageUtils = new PageUtils(infocollList, total);
        return pageUtils;
    }


    /**
     * 新增软著信息采集
     */
    @GetMapping("/add")
    String add(HttpServletRequest request, Model model) {
        String seckey = request.getParameter("seckey");

        Map map = new HashMap();
        map.put("status", 1);
        map.put("liveAddress", seckey);
        List<UserDO> list = userService.list(map);
        if (list.size() > 0) {
            UserDO userDO = list.get(0);
            model.addAttribute("userDO", userDO);
            return "biz/infocomplete/add";
        }
        return "404";
    }

    /**
     * 编辑软著信息采集
     */
    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Integer id, Model model) {
        InfocollDO infocoll = infocollService.get(id);
        model.addAttribute("infocoll", infocoll);
        return "biz/infocomplete/edit";
    }


    /**
     * 保存内部人员提交的软著信息采集
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(InfocollDO infocoll) {
        if (infocollService.save(infocoll) > 0) {

            return R.ok();
        }
        return R.error();
    }

    /**
     * 保存客户提交的软著信息采集
     */
    @ResponseBody
    @PostMapping("/csave")
    public R cstmSave(InfocollDO infocoll) {

        if (infocollService.save(infocoll) > 0) {
            SoftwareDO software = new SoftwareDO();
            software.setSn("");
            software.setStatus(0);
            software.setCopyrightOwner(infocoll.getCopyrightOwner());
            software.setSoftwareName(infocoll.getSoftwareName());
            software.setBusinessMan(Integer.valueOf(infocoll.getBusinessMan()));

            softwareService.save(software);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改软著信息采集
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(InfocollDO infocoll) {
        String softwareName = infocollService.get(infocoll.getId()).getSoftwareName().trim();
        if (softwareName.equals(infocoll.getSoftwareName().trim())) {
            infocoll.setSoftwareName(softwareName);
            infocoll.setStatus(1);
            infocollService.update(infocoll);
            infocoll = infocollService.get(infocoll.getId());
            //添加到正常业务
            SoftwareDO softwareDO = new SoftwareDO();
            softwareDO.setAgentCompany(agentService.get(infocoll.getAgent()).getName());
            softwareDO.setCopyrightOwner(infocoll.getCopyrightOwner());
            softwareDO.setSoftwareName(softwareName);
            softwareDO.setWorkDays(infocoll.getWorkDays());
            softwareDO.setBusinessType(1);//
            softwareDO.setType(1);//type: 1,写材料，0,不用写材料
            softwareDO.setStatus(0);//status:0,未提交;1,已提交
            softwareDO.setBusinessMan(infocoll.getBusinessMan());
            softwareService.save(softwareDO);

            return R.ok();
        } else {
            return R.error("您提供的软件名称跟客户提交的软件名称不一致，请确认后再修改。");
        }

    }

    /**
     * 删除软著信息采集
     */
    @PostMapping("/remove")
    @ResponseBody
    public R remove(Integer id) {
        if (infocollService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除软著信息采集
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        infocollService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 批量分配
     */
    @PostMapping("/distribute")
    @ResponseBody
    public R distribute(@RequestParam("ids[]") Integer[] ids, @RequestParam("material") Integer material) {
        infocollService.distribute(ids, material);
        return R.ok();
    }
}
